package com.ohos.carbon.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Point;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;

import java.util.Map;
import java.util.Optional;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ChartViewDemo extends AbilitySlice {

    private DirectionalLayout myLayout = new DirectionalLayout(this);

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(this.getContext());
        Constant.point = new Point();
        display.get().getSize(Constant.point);
        ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_PARENT);
        myLayout.setLayoutConfig(config);

        CharView charView = new CharView(this);
        charView.setLayoutConfig(config);
        charView.setInfo(new String[]{"7-11", "7-12", "7-13", "7-14", "7-15", "7-16", "7-17"}, // X轴刻度
                new String[]{"", "10", "20", "30", "40", "50"}, // Y轴刻度
                new String[]{"15", "23", "10", "36", "45", "40", "12"}, // 数据
                "line chart");
        myLayout.addComponent(charView);
        super.setUIContent(myLayout);

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Line chart")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Line chart")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Line chart", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Line chart", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Line chart", 1);
            pf.flushSync();
        }
    }
}
